<?php
/**
 * Date: 2024.8
 * Author: kahn
 */
namespace  service\uc;

use config\Config;
use models\uc\OauthModel;
use service\uc\oauth\WeixinMicroClient;

class OauthService extends  \Service {

    /**
     * 获取小程序登录客户端
     * @return WeixinMicroClient
     */
    public static function getWxMicroClient(): WeixinMicroClient
    {
        return new WeixinMicroClient(Config::WX_PLATFORM[Config::WX_PLATFORM_MICRO]);
    }

    static function login($unionId, $mobile, &$userId, &$token): int
    {
        $oauthRelation = function ($userId) use($unionId){
            $oauth = OauthModel::where('unionId', $unionId)
                ->where('userId', $userId)
                ->findOrEmpty();
            if($oauth->isEmpty()){
                $oauth = OauthModel::create(['unionId' => $unionId, 'userId' => $userId]);
                return !$oauth->isEmpty();
            }

        };

        return UserService::login($mobile, Config::REGISTER_LOGIN_TYPE_WX, $userId, $token, $oauthRelation);
    }
}